﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using r4u.business.logic.layer.RECRUIT;
using r4u.data.access.layer;
using r4u.data.access.layer.REGISTER;

namespace r4u.business.logic.layer.ALERT
{
    public class Mail
    {
        public bool SendMail(MailMessage mail)
        {
            try
            {
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("recruitments4uu@gmail.com");
                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("recruitments4uu", "neru2006");
                SmtpServer.EnableSsl = true;
                SmtpServer.Send(mail);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool SendVerificationCode(string email)
        {

            UserDAO userDao = new UserDAO();
            string activation_code = userDao.GetCode(email);
            

            MailMessage mail = new MailMessage();
            mail.To.Add(email);
            mail.Subject = "R4U Account Verification";
            mail.Body = String.Format(@"Thank you for registering with R4U. You may use following verification code
to verify your account.

Verification Code: {0}

Happy Recruiting,
R4U Team.", activation_code);
            return this.SendMail(mail);
        }

        public bool SendPasswordResetLink(string email)
        {
            UserDAO userDao = new UserDAO();

            if (userDao.CheckExist(email))
            {
                var callbackUrl = String.Format("http://localhost:49542/Reset/Index/{0}", userDao.GetCode(email));

                MailMessage mail = new MailMessage();
                mail.To.Add(email);
                mail.Subject = "R4U Password Reset";
                mail.Body = String.Format(@"Hi,

To reset your password please click <a href=""{0}"">here</a>.

Happy Recruiting,
R4U Team.", callbackUrl);
                mail.IsBodyHtml = true;

                return this.SendMail(mail);
            }
            else
            {
                return false;
            }
        }


        public void SendApplicants(string email, string vacancy_id)
        {
            Vacancy vacancy = new Vacancy();
            VACANCY v = vacancy.GetVacancyWithSkills(vacancy_id);
            Candidate[] candidates = vacancy.GetApplicants(vacancy_id);

            MailMessage mail = new MailMessage();
            mail.Subject = String.Format("Vacancy ID: {0} - {1}", v.vacancy_id, v.vacancy_for);
            mail.To.Add(email);
            mail.IsBodyHtml = true;

            StringBuilder sb = new StringBuilder();
            sb.Append("<h2>" + mail.Subject + "</h2><br/><hr>");
            sb.Append("Following applicants have applied to your job vacancy. Please login to application to view them all.<br/><br/><br/>");
            sb.Append("<table>");

            foreach (Candidate candidate in candidates)
            {
                sb.Append("<tr style='padding:7px; border:#4e95f4 1px solid;'>");
                sb.Append("<td width='5%'><img src='" + candidate.PictureURL + @"'/></td>");
                sb.Append("<td width='95%'><b>" + candidate.FirstName + " " + candidate.LastName + "</b><br/>" + candidate.Headline + "</td>");
                sb.Append(@"<td><a href='http://localhost:49542/DashBoard/Index'><button style='background: #3498db;
                                  background-image: -webkit-linear-gradient(top, #3498db, #2980b9);
                                  background-image: -moz-linear-gradient(top, #3498db, #2980b9);
                                  background-image: -ms-linear-gradient(top, #3498db, #2980b9);
                                  background-image: -o-linear-gradient(top, #3498db, #2980b9);
                                  background-image: linear-gradient(to bottom, #3498db, #2980b9);
                                  -webkit-border-radius: 28;
                                  -moz-border-radius: 28;
                                  border-radius: 28px;
                                  font-family: Arial;
                                  color: #ffffff;
                                  font-size: 12px;
                                  padding: 10px 20px 10px 20px;
                                  text-decoration: none;'>View...</button></a></td>");
                sb.Append("</tr>");
            }

            sb.Append("</table>");
            mail.Body = sb.ToString();


            UserDAO userDao = new UserDAO();
            if (!string.IsNullOrEmpty(userDao.GetUser(email).receive_mail) && userDao.GetUser(email).receive_mail.Equals("Y"))
                this.SendMail(mail);
        }

    }
}